let amap = require("../../utils/amap.js")
var app = getApp();

Page({
  data: {
    name: "",
    desc: "",
    markers: [{
      iconPath: "",
      id: 0,
      latitude: "",
      longitude: "",
      width: 23,
      height: 33
    }, {
      iconPath: "",
      id: 0,
      latitude: "",
      longitude: "",
      width: 24,
      height: 34
    }],
    distance: "",
    location: "",
    destination:"",
    polyline: []
  },

  onLoad(options){
    var indexview = options.indexview;
    this.setData({
      destination: app.globalData.park_list[indexview].address
    })
    this.getRegeo(options);
    this.getInputtips(options); 
  },

  onReady: function(){
    var that = this;
    wx.getSetting({
      success: function(res){
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true){
          wx.showModal({
            title: '是否授权当前位置',
            content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
            success: function(res) {
              if(res.cancel){
                console.info("授权失败,返回数据");
              } else if(res.confirm) {
                wx.openSetting({
                  success:function(data){
                    console.log(data.authSetting['scope.userLocation']);
                    if (data.authSetting['scope.userLocation'] == true){
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 5000
                      })
                      that.getRegeo(that);
                    } 
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          that.getRegeo(that);
        }
      }
    })
  },

  getRegeo(options){
    var that = this;
    amap.getRegeo().then( d=> {
      // console.log(d);
      var obj = d[0];
      that.data.markers[0] = {
        iconPath: "../../images/mapicon_navi_s.png",
        id: 0,
        latitude: obj.latitude,
        longitude: obj.longitude,
        width: 23,
        height: 33
      }
      that.setData({
        name: obj.name,
        desc: obj.regeocodeData.addressComponent,
        markers: that.data.markers
      })
      console.log(that.data.markers[0]);
      that.getRoute();
    })
  },

  getInputtips(options){
    var that = this;
    let keywords = that.data.destination;
    console.log(keywords);
    // let city = '杭州';
    amap.getInputtips(keywords).then(res =>{
      that.data.markers[1] = {
        iconPath: "../../images/mapicon_navi_e.png",
        id: 0,
        latitude: res.tips[0].location.split(',')[1],
        longitude: res.tips[0].location.split(',')[0],
        width: 23,
        height: 33
      }
      that.setData({
        name: res.tips[0].name,
        des: res.district + res.address,
        markers: that.data.markers
      })
      console.log(that.data.markers);
      that.getRoute();
    })
  },

  getRoute(options) {
    let origin = `${this.data.markers[0].longitude}, ${this.data.markers[0].latitude}`;
    let destination = `${this.data.markers[1].longitude},${this.data.markers[1].latitude}`;
    let city = '杭州';
    // console.log(destination);
    amap.getDrivingRoute(origin, destination, city).then(data=> {
      var points = [];
      if (data.paths && data.paths[0] && data.paths[0].steps) {
        var steps = data.paths[0].steps;
        wx.setStorageSync("steps", steps);
        for (var i = 0; i < steps.length; i++) {
          var poLen = steps[i].polyline.split(';');
          for (var j = 0; j < poLen.length; j++) {
            points.push({
              longitude: parseFloat(poLen[j].split(',')[0]),
              latitude: parseFloat(poLen[j].split(',')[1])
            })
          }
        }
      }
      this.setData({
        polyline: [{
          points: points,
          color: "#0091ff",
          width: 6
        }]
      });
      if (data.paths[0] && data.paths[0].distance) {
        if (data.paths[0].distance > 1000){
          data.paths[0].distance = data.paths[0].distance / 1000.0;
          this.setData({
            distance: data.paths[0].distance + '公里'
          })
        } else{
          this.setData({
            distance: data.paths[0].distance + '米'
          })
        }
      }
    })
    .catch(e => {
      console.log(e);
    })
  },

  goDetail() {
    let url = `/pages/info/info`;
    wx.navigateTo({ url });
  },

  nav() {
    let { latitude, longitude, name, desc } = this.data.markers[1];
    wx.openLocation({
      latitude: +latitude,
      longitude: +longitude,
      name,
      address: desc
    });
  }
})